package hanjoapp.client;

import hanjoapp.shared.FieldVerifier;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Register extends FlowPanel {

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final RegistrationServiceAsync registrationService = GWT.create(RegistrationService.class);

	final Button sendButton;
	
	public Register() {
		
		// Button sendButton = new Button("Send");
		addFormTextField("Login", "login");
		addFormPwdField("Password", "pwd");
		addFormPwdField("Confirm password", "pwdConf");
		addFormTextField("First name", "firstName");
		addFormTextField("Last name", "lastName");
		addFormTextField("E-Mail", "email");
		sendButton = new Button("Send");
		add(sendButton);
		
		// Create the popup dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Creating user:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);

		
		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				sendButton.setEnabled(true);
				sendButton.setFocus(true);
			}
		});

		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				sendDataToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendDataToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a response.
			 */
			private void sendDataToServer() {
				System.out.println("test");
				// First, we validate the input.
				String login = "test";
				String pwd = "test";
				String pwdConfirm = "test";
				String firstName = "test";
				String lastName= "test";
				String email = "test";
				if (!FieldVerifier.isValidLogin(login)) {
					// loginErrorLabel.setText("Please enter at least four characters");
					return;
				}
				/* if (!FieldVerifier.loginAvailable(login)) {
					loginErrorLabel.setText("This login is already taken");
					return;
				} */
				if (!FieldVerifier.isValidPassword(pwd, pwdConfirm)) {
					// loginErrorLabel.setText("Your passwords do not match");
					return;
				}

				// Then, we send the input to the server.
				sendButton.setEnabled(false);
				serverResponseLabel.setText("");
				registrationService.registrationServer(login, pwd, pwdConfirm, email, firstName, lastName, new AsyncCallback<User>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox
										.setText("Remote Procedure Call - Failure");
								serverResponseLabel
										.addStyleName("serverResponseLabelError");
								dialogBox.center();
								closeButton.setFocus(true);
							}

							public void onSuccess(User result) {
								dialogBox.setText("Remote Procedure Call");
								serverResponseLabel
										.removeStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML("User created");
								dialogBox.center();
								closeButton.setFocus(true);
							}
						});
			}
		}


		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		// loginField.addKeyUpHandler(handler);
	}
	
	private void addFormTextField(String label, String name) {

		TextBox field = new TextBox();
		field.setName(name);
		Label fieldLabel = new Label(label);
		Label errorLabel = new Label();
		add(fieldLabel);
		add(field);
		add(errorLabel);
	}

	private void addFormPwdField(String label, String name) {

		PasswordTextBox field = new PasswordTextBox();
		field.setName(name);
		Label fieldLabel = new Label(label);
		Label errorLabel = new Label();
		add(fieldLabel);
		add(field);
		add(errorLabel);
	}
}
